<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // reduce方法的练习
      // 练习1
      const arr = [1, 2, 3, 4, 5];
      // 使用reduce方法计算数组之和
      function getNum(arr) {
        let arrSum = arr.reduce((sum, value) => sum += value, 0);
        console.log(arrSum);
        return arrSum;
      }
      getNum(arr); // 15

      // 练习2
      let arr1 = [
        {
          price: 100,
          count: 10,
          name: "手机",
        },
        {
          price: 200,
          count: 20,
          name: "电脑",
        },
        {
          price: 400,
          count: 40,
          name: "键盘",
        },
      ];
      // 定义一个函数
      function getAllPrice(arr) {
        let priceRes = arr.reduce( (priceSum, obj) => priceSum += obj.price, 0)
        let countRes = arr.reduce( (priceSum, obj) => priceSum += obj.count, 0)
        console.log(`手机电脑键盘的总价为${priceRes}元`)
        console.log(`手机电脑键盘的总数量为${countRes}个`)
        return { 
            priceRes,
            countRes
         }
    }
    
      getAllPrice(arr1); // '手机电脑键盘的总价为700元'
    </script>
  </body>
</html>
